/**
 * Created by benz on 2017/3/19.
 */
module.exports = app => {
    return class UserController extends app.Controller {
        * index(ctx) {
            const params=ctx.query || "";
            const pagination=params.pagination || "";//分页内容
            const pageNum=pagination.pageNum || 1; //页码
            const pageLength=pagination.pageLength || 12;//每页条数
            // if(params.screen){
            //     var screen=JSON.parse(params.screen);//条件
            // }else{
            //     var screen=params.screen;//条件
            // }
            // console.log(screen);
            const screen=JSON.parse(params.screen) || "";//条件
            const orderby=params.orderby || "";//排序
            const offesrNum=(pageNum-1)*pageLength;//偏移量


            const articles = yield this.ctx.model.Article.findAndCountAll({
                limit:pageLength,offset:offesrNum,where:screen,order:orderby,include:[{model:ctx.model.Menu,attributes:["title"]}]
            });
            ctx.body={
                articles:articles.rows,
                params:{
                    pagination:{
                        pageNum:pageNum,
                        pageTotal:articles.count,
                        pageLength:pageLength
                    },
                    orderby:orderby,
                    screen:screen
                }
            };
        }
        * create(ctx) {
            const body=ctx.request.body;
            const articles=yield  this.ctx.model.Article.create({
                userId:ctx.user.id,title:body.title,menuId:body.menuId,remark:body.remark,content:body.content,
            });
            if(articles){
                ctx.body={
                    articles:articles
                }
            }else {
                ctx.status=208;
                ctx.body={
                    error:"添加失败，请稍后再试。"
                }
            }
        }
        * show(ctx){
            yield this.ctx.model.Article.update({views:ctx.model.literal('views+1')},{where:{id:ctx.params.id}});
            const article=yield this.ctx.model.Article.findById(ctx.params.id);
            this.ctx.body=article;
        }
        * update(ctx) {
            const body=ctx.request.body;
            console.log(body.userId);
            if(body.userId===ctx.user.id){//当前用户是该文章作者
                const articles=yield  this.ctx.model.Article.update({
                    userId:ctx.user.id,title:body.title,menuId:body.menuId,remark:body.remark,content:body.content,
                },{where:{id:ctx.params.id}});
                if(articles){
                    ctx.body={
                        articles:articles
                    }
                }else {
                    ctx.status=208;
                    ctx.body={
                        error:"添加失败，请稍后再试。"
                    }
                }
            }else {
                ctx.status=208;
                ctx.body={
                    error:"没有权限操作。"
                }
            }

        }
        * destroy(ctx) {
            const articles=yield this.ctx.model.Article.destroy({
                where:{id:ctx.params.id}
            });
            if(articles>0){
                ctx.status=204;
            }else{
                ctx.status=208;
                ctx.body={error:"删除失败，请稍后再试。"};
            }
        }
    }
}